﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace Maticsoft.Web
{
    public partial class ProductDetail : PageBaseMember
    {
        protected int cid = 0;
        protected int pid = 0;
        protected string nav = string.Empty;
        protected Model.Products proModel = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxProcess();

            int.TryParse(Request.QueryString["cid"], out cid);
            int.TryParse(Request.QueryString["id"], out pid);

            if (!IsPostBack)
            {
                GetDetail();
                GetNav(cid);
                BindGood();
                BindHistory();
                SetHistory();
                BindSmallPic();
                BindComments();
            }
        }

        protected void AjaxProcess()
        {
            if (Request.QueryString["ope"] == "addnum")
            {
                Response.Write(AddNum());
                Response.End();
            }
        }

        protected string AddNum()
        {
            int productId = 0;
            int num = 0;
            int.TryParse(Request.QueryString["productId"], out productId);
            int.TryParse(Request.QueryString["num"], out num);
            if (productId > 0)
            {
                //BLL.ShoppingCar _ShoppingCarBLL = new BLL.ShoppingCar();
                //BLL.Products _ProductsBLL = new BLL.Products();
                //if (num != 0)
                //{
                //    Model.Products _ProductsModel = _ProductsBLL.GetModel(productId);
                //    Model.Oitems _OitemsModel = new Model.Oitems();
                //    _OitemsModel.ProductId = productId;
                //    _OitemsModel.ProductPic = _ProductsModel.Pic;
                //    _OitemsModel.ProductNum = num;
                //    _OitemsModel.ProductName = _ProductsModel.ProductName;
                //    _OitemsModel.SalePrice = _ProductsModel.SalePrice;
                //    _OitemsModel.RealPrice = _ProductsModel.SalePrice;
                //    _OitemsModel.TotalPrice = _ProductsModel.SalePrice * num;
                //    _OitemsModel.TaxFee = 0;
                //    _OitemsModel.InsureFee = 0;
                //    _OitemsModel.DeliveryFee = 0;
                //    _OitemsModel.ProductDesc = Request.QueryString["lay"];
                //    int res = _ShoppingCarBLL.UpdateShoppingCar(_OitemsModel);

                //    return res.ToString();
                //}

                if (string.IsNullOrEmpty(this.UserId))
                {
                    return "-5";
                }

                BLL.Products proBLL = new BLL.Products();
                Model.Products proModel = proBLL.GetModel(productId);
                BLL.ShoppingInfo carBLL = new BLL.ShoppingInfo();
                Model.ShoppingInfo carModel = new Model.ShoppingInfo();
                carModel.UserId = int.Parse(this.UserId);
                carModel.ProductId = productId;
                carModel.ProductNum = num;
                carModel.ProductDesc = Request.QueryString["lay"];
                carModel.IsActive = 1;
                carModel.CreateTime = DateTime.Now;

                return carBLL.AddShoppingInfo(carModel).ToString();
            }

            return "0";
        }
        

        protected string GetNav(int id)
        {
            string curr = string.Empty;
            BLL.Categorys catBLL = new BLL.Categorys();
            Model.Categorys catModel = catBLL.GetModel(id);
            curr = "<span> > </span><a href=\"ProductList.aspx?cid=" + catModel.CategoryId + "\" class=\"fs-14\">" + catModel.CategoryName + " </a>";
            if (catModel.ParentId != 0)
            {
                nav = GetNav(catModel.ParentId) + curr;
            }
            else
            {
                nav = nav + curr;
            }

            return nav;
        }


        protected void GetDetail()
        {
            if (pid > 0)
            {
                BLL.Products proBLL = new BLL.Products();
                proModel = proBLL.GetModel(pid);

                Title = proModel.ProductName;
                MetaKeywords = proModel.ProductName;
                MetaDescription = proModel.ProductName;

                if (proModel == null)
                {
                    proModel = new Model.Products();
                }
                else
                {
                    if (proModel.IsHid == 1 || proModel.IsActive == 0)
                    {
                        Response.Write("该产品已经下架");
                        Response.End();
                    }
                }
            }
        }

        protected void BindComments()
        {
            BLL.Comments coBLL = new BLL.Comments();
            string strWhere = "ProductId=" + pid + " and IsActive=1 and ShowStatus=1";
            int recordCount = coBLL.GetRecordCount(strWhere);
            ltlCount.Text = recordCount.ToString();
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = coBLL.GetListByPageV(strWhere, "CommentId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            rptComments.DataSource = ds.Tables[0].DefaultView;
            rptComments.DataBind();
        }

        protected string GetStar(object obj)
        {
            int points = 0;
            int.TryParse(Convert.ToString(obj), out points);
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= 5; i++)
            {
                if (i <= points)
                {
                    sb.Append("<i><img src=\"img/star-checked.png\" /></i>");
                }
                else
                {
                    sb.Append("<i><img src=\"img/star-nochecked.png\" /></i>");
                }
            }

            return sb.ToString();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindComments();
        }

        protected void BindSmallPic()
        {
            BLL.ProductPics picBLL = new BLL.ProductPics();
            DataSet ds = picBLL.GetList("ProductId=" + pid + " and IsActive=1 order by ProductPicId asc");
            rptSmallPic.DataSource = ds.Tables[0].DefaultView;
            rptSmallPic.DataBind();

            if (ds.Tables[0].Rows.Count > 0)
            {
                string path = ds.Tables[0].Rows[0]["Url"].ToString();
                midimg.Src = "uploadpic/" + path.Substring(0, path.IndexOf(".")) + "_m." + path.Substring(path.IndexOf(".") + 1);
            }
        }

        /// <summary>
        /// 推荐产品
        /// </summary>
        protected void BindGood()
        {
            BLL.Products proBLL = new BLL.Products();
            DataSet ds = proBLL.GetList(5, "IsGood=1 and IsHid=0", "orderNo desc");
            rptHotProduct.DataSource = ds.Tables[0].DefaultView;
            rptHotProduct.DataBind();
        }
        /// <summary>
        /// 添加浏览历史
        /// </summary>
        private void SetHistory()
        {
            string proIds = string.Empty;
            if (Request.Cookies["history"] != null)
            {
                proIds = Request.Cookies["history"].Value;
            }
            if (!string.IsNullOrEmpty(proIds))
            {
                proIds = pid + "," + proIds;
            }
            else
            {
                proIds = pid.ToString();
            }


            Response.Cookies["history"].Value = proIds;
        }

        /// <summary>
        /// 浏览历史
        /// </summary>
        protected void BindHistory()
        {
            if (Request.Cookies["history"] != null)
            {
                string proIds = Request.Cookies["history"].Value;
                string[] arrIds = proIds.Split('|');
                proIds = string.Empty;
                for (int i = 0; i < (arrIds.Length>5 ? 5 : arrIds.Length); i++)
                {
                    if (!string.IsNullOrEmpty(proIds))
                    {
                        proIds += ",";
                    }
                    proIds += arrIds[i];
                }
                BLL.Products proBLL = new BLL.Products();
                DataSet ds = proBLL.GetList(5, "ProductId in(" + proIds + ") and IsHid=0 and IsActive=1", "ProductId desc");
                rptHistory.DataSource = ds.Tables[0].DefaultView;
                rptHistory.DataBind();
            }
        }
    }
}